#include "widget.h"
#include "ui_widget.h"
#include <QString>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //步骤1：给label安装事件过滤器 this：当前窗口安装事件过滤器
    ui->label->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject* obj, QEvent* e)
{
    if(obj == ui->label) //判断条件
    {
        if(e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* event = static_cast<QMouseEvent*>(e);
            QString str = QString("事件过滤器中按下鼠标：x = %1, y = %2").arg(event->x()).arg(event->y());
            qDebug() << str.toUtf8().data();

            return true;
        }
    }

    // 其他交给父类处理
    return QWidget::eventFilter(obj, e);
}
